﻿namespace WordCloudClient.ViewModels
{
    public class ViewRuleViewModel : DataGridCommonVM
    {
        public DelegateCommand ExecuteSaveDelegateCommand { get; set; }
        public DelegateCommand ExecuteSelectMaskImageDelegateCommand { get; set; }
        public ViewRuleViewModel(SystemService systemService, WinEditHelper winEditHelper) : base(systemService, winEditHelper)
        {
            ExecuteSaveDelegateCommand = new DelegateCommand(ExecuteSave);
            ExecuteSelectMaskImageDelegateCommand = new DelegateCommand(ExecuteSelectMaskImage);
        }
        private RuleEntity _ruleEntity = new();
        public RuleEntity RuleEntity
        {
            get { return _ruleEntity; }
            set
            {
                _ruleEntity = value;
                RaisePropertyChanged(nameof(RuleEntity));
            }
        }
        public override void LoadData()
        {
            RuleEntity.MaxCount =Convert.ToInt32(INIFileLauncher.IniReadValue("Rule", "MaxCount"));
            RuleEntity.Height = Convert.ToInt32(INIFileLauncher.IniReadValue("Rule", "Height"));
            RuleEntity.Width = Convert.ToInt32(INIFileLauncher.IniReadValue("Rule", "Width"));
            RuleEntity.FilterLength = Convert.ToInt32(INIFileLauncher.IniReadValue("Rule", "FilterLength"));
            RuleEntity.MaskImagePath = INIFileLauncher.IniReadValue("Rule", "MaskImagePath");
        }
        public void ExecuteSave()
        {
            INIFileLauncher.IniWriteValue("Rule", "MaxCount", RuleEntity.MaxCount.ToString());
            INIFileLauncher.IniWriteValue("Rule", "Height", RuleEntity.Height.ToString());
            INIFileLauncher.IniWriteValue("Rule", "Width", RuleEntity.Width.ToString());
            INIFileLauncher.IniWriteValue("Rule", "FilterLength", RuleEntity.FilterLength.ToString());
            INIFileLauncher.IniWriteValue("Rule", "MaskImagePath", RuleEntity.MaskImagePath);
            MessageTips.SuccessMessage("保存成功");
        }
        public void ExecuteSelectMaskImage()
        {
            string location = CommonHelper.UrlRelativeToAbsolute($"./Mask");
            string path = CommonHelper.GetFileAddress("选择外观", "图片文件(*.png*|*.png|", location);
            RuleEntity temp = RuleEntity;
            temp.MaskImagePath = path;
            RuleEntity = temp;
        }
    }
}
